package chj.Bookmanage;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.*;

import chj.BookInformation.Bookop;
import chj.bookface.BKJPanel;
import chj.Database.CDataBase;
import chj.Database.StringUtil;

public class Bdelete {

    public static void BookDeleteFram() {

        JFrame frame = new JFrame("书籍删除");
        frame.setLayout(null);

        BKJPanel pan = new BKJPanel("src/chj/image/bg.jpg");  //背景图地址
        pan.setLayout(null);
        //开面板，方便设置位置
        JPanel pan1 = new JPanel();
        JPanel pan2 = new JPanel();
        JPanel pan5 = new JPanel();

        //文本框
        JTextField textBno = new JTextField();//

        //提示框
        JLabel label1 = new JLabel("书籍删除");
        JLabel label2 = new JLabel("书 籍 编 号  ");

        //按钮
        JButton button1 = new JButton("删除");
        Font font = new Font("宋体", Font.BOLD, 50);//标题字体大小
        Font f = new Font("宋体", Font.BOLD, 25);//提示框字体大小
       // 设置第按钮的样式
        button1.setBackground(new Color(34, 139, 34)); // 设置按钮背景色
        button1.setForeground(Color.WHITE); // 设置按钮文字颜色
        button1.setBorder(BorderFactory.createLineBorder(new Color(34, 139, 34))); // 设置边框颜色
        button1.setFocusPainted(false); // 去掉按钮聚焦时的边框
        button1.setOpaque(true);
        //button2.setBorderRadius(20); // 设置圆角

        // 设置按钮悬停效果
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button1.setBackground(new Color(50, 205, 50)); // 悬停时改变背景色
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button1.setBackground(new Color(34, 139, 34)); // 鼠标离开时恢复背景色
            }
        });
            //设置文本框的大小
            textBno.setPreferredSize(new Dimension(200, 30));
            button1.setPreferredSize(new Dimension(90, 40));

            //设置界面所有字体大小，包括标题、提示框字体和文本框
            label1.setFont(font);//设置标题字体
            label2.setFont(f);
            textBno.setFont(f);
            button1.setFont(f);
           // 设置文本框外观
           textBno.setBackground(new Color(240, 240, 240));  // 设置背景颜色
           textBno.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 2));  // 边框颜色

            //向面板中添加组件
            pan1.add(label1);
            pan2.add(label2);
            pan2.add(textBno);
            pan5.add(button1);

            //设置面板位置
            pan1.setOpaque(false);
            pan2.setOpaque(false);
            pan5.setOpaque(false);
            pan1.setBounds(140, 50, 430, 60);
            pan2.setBounds(130, 170, 430, 50);
            pan5.setBounds(340, 300, 100, 50);//放查询按钮

            //添加面板
            pan.add(pan1);
            pan.add(pan2);
            pan.add(pan5);
            frame.setContentPane(pan);

            //用户查询监听
            button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    String textbno = textBno.getText();
                    if (StringUtil.isEmpty(textbno)) {
                        JOptionPane.showMessageDialog(null, "书籍编号不能为空！");
                        return;
                    }

                    Connection con = null;
                    CDataBase connection = new CDataBase();
                    Bookop bookdao = new Bookop();

                    try {
                        con = connection.getConnection();
                        int currentUser = bookdao.delete(con, textbno);//若找并删除，返回值为1

                        if (currentUser == 1) {
                            JOptionPane.showMessageDialog(null, "删除成功！");
                        } else {
                            JOptionPane.showMessageDialog(null, "未找到该书籍图书！");
                        }
                    } catch (Exception evt) {
                        evt.printStackTrace();
                    }
                }
            });

            //窗口设置
            frame.setBounds(400, 200, 750, 550);
            frame.setVisible(true);
    }
}
